#!/usr/bin/env python3.6
# -*- encoding: utf-8 -*-
'''
@File    :   spiderrecv.py
@Time    :   2020/08/19 00:05:10
@Author  :   Tang Jing 
@Version :   1.0.0
@Contact :   yeihizhi@163.com
@License :   (C)Copyright 2020
@Desc    :   None
'''

# here put the import lib
import datetime
import threading
from TDhelper.db.mysql.base import db_pool
from TDhelper.Decorators.log import logging_setup, logging, SYS_LOGS
from general.db import db_helper
from api.logic.models import categoryModel, movieModel
from src.settings import LOGGING, DATABASES
import json
import re

# code start
logging_setup(LOGGING)

db_event = threading.Event()


def saveTopType(category):
    category= category.encode('utf-8').decode('unicode_escape')
    category= categoryModel().getObj(json.loads(category))
    m_type_name = category.categoryName
    session = db_helper.getSession()
    if session:
        try:
            cursor = session.cursor()
            m_type_name = m_type_name.replace(" ", "")
            if m_type_name:
                cursor.execute(
                    "select *from category where name= \"{0}\"".format(m_type_name))
                m_data = cursor.fetchall()
                if cursor.rowcount <= 0:
                    logging.info("- save type:{0}".format(m_type_name))
                    sql = "insert into category(name, parent_id) values(\"{0}\",0)".format(
                        m_type_name)
                    cursor.execute(sql)
                    session.commit()
                m_data = None
        except Exception as e:
            logging.error(e)
        finally:
            cursor.close()
            session.close()
            db_event.set()
    else:
        db_event.clear()
        db_event.wait()
        saveTopType(m_type_name)


def saveChildType(category):
    category= category.encode('utf-8').decode('unicode_escape')
    category= categoryModel().getObj(json.loads(category))
    m_top_type = category.categoryParentName
    m_child_type = category.categoryName
    session = db_helper.getSession()
    if session:
        try:
            cursor = session.cursor()
            m_top_type = m_top_type.replace(" ", "")
            m_child_type = m_child_type.replace(" ", "")
            if m_top_type:
                if m_child_type:
                    cursor.execute(
                        "select *from category where name=\"{0}\"".format(m_child_type))
                    data = cursor.fetchall()
                    if cursor.rowcount <= 0:
                        cursor.execute(
                            "select *from category where name=\"{0}\"".format(m_top_type))
                        data = cursor.fetchall()
                        if cursor.rowcount >= 0:
                            logging.info(
                                "- save child type:{1}, parent type is {0}".format(m_top_type, m_child_type))
                            sql = "insert into category(name, parent_id) values(\"{0}\",{1})".format(
                                m_child_type, data[0][0])
                            cursor.execute(sql)
                            session.commit()
        except Exception as e:
            logging.error(e)
        finally:
            cursor.close()
            session.close()
            db_event.set()
    else:
        db_event.clear()
        db_event.wait()
        saveChildType(category)


def saveMsg(movieMsg):
    movieMsg= movieMsg.encode('utf-8').decode('unicode_escape')
    movieMsg= movieModel().getObj(json.loads(movieMsg))
    sources = movieMsg.source
    sources_index = movieMsg.sourceIndex
    kwargs = movieMsg.movieData
    session = db_helper.getSession()
    if session:
        sql=''
        try:
            cursor = session.cursor()
            m_c_type = kwargs['c_type']
            if m_c_type:
                m_c_type= m_c_type.split(" ")
            if len(m_c_type) > 0:
                m_c_type = m_c_type[0]
            m_c_type = m_c_type.replace(" ", "")
            m_c_type = m_c_type.replace('片', "")
            cursor.execute(
                "select *from category where name=\"{0}\"".format(m_c_type))
            data = cursor.fetchone()
            type_id = data[0]
            type_enable= data[3]
            m_name = kwargs["name"]
            m_name = m_name.replace(" ", "")
            m_name = re.sub(r"[0]\d*", "", m_name, count=0, flags=0)
            sql = "select *from movie_list where name=\"{0}\" and category_id={1}".format(
                m_name, type_id)
            cursor.execute(sql)
            data = cursor.fetchone()
            movie_id = 0
            if cursor.rowcount >= 0:
                # old msg update
                movie_id = data[0]
                logging.info(
                    "- update movie message.movie name is {0}".format(m_name))
                if data[13]:
                    sql = "update movie_list set last_update_time=\"{0}\" where id=\"{1}\"".format(
                    str(datetime.datetime.now()), movie_id)
                else:
                    sql= "update movie_list set last_update_time=\"{0}\", cover=\"{1}\" where id=\"{2}\"".format(
                    str(datetime.datetime.now()), kwargs['img'],movie_id)
                cursor.execute(sql)
                session.commit()
            else:
                # new msg insert
                year = kwargs["year"]
                year = year.lstrip().rstrip()
                last_update_time = kwargs["update_date"]
                try:
                    last_update_time = last_update_time.lstrip().rstrip()
                    last_update_time = datetime.datetime.strptime(
                        last_update_time, '%Y-%m-%d %H:%M:%S')
                except Exception as e:
                    pass
                # new msg insert
                logging.info(
                    "- insert a new movie message.movie name is {0}".format(m_name))
                sql = "insert into movie_list(name,remark,director,performer,number,category_id,country,language,year,last_update_time,tag,description,cover,alias,enable) values(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",{5},\"{6}\",\"{7}\",\"{8}\",\"{9}\",\"{10}\",\"{11}\",\"{12}\",\"{13}\",{14})".format(
                    m_name, kwargs['remark'], kwargs['director'], kwargs['performer'], kwargs['number'], type_id, kwargs['country'], kwargs['language'], year, last_update_time, kwargs['tag'], kwargs['description'], kwargs['img'], kwargs['alias'],type_enable)
                cursor.execute(sql)
                session.commit()
                movie_id = cursor.lastrowid
            if movie_id > 0:
                sql= "select *from movie_source_maps where movie={0} and url='{1}'".format(movie_id,movieMsg.currentUrl)
                cursor.execute(sql)
                ret_data= cursor.fetchall()
                if cursor.rowcount<=0:
                    sql= "insert into movie_source_maps(name,url,`key`,movie) values('{0}','{1}','{2}',{3})".format(sources,movieMsg.currentUrl,movieMsg.key,movie_id)
                    cursor.execute(sql)
                    session.commit()
                # play url process
                urls = kwargs["play_urls"].split('~#~')
                source_id = sources_index + 1
                for m_url in urls:
                    m_url_offset= 0
                    sql= "select *from play_url_list_count where sources={0} and spider_source_index={1} and movie={2}".format(source_id,sources_index,movie_id)
                    cursor.execute(sql)
                    m_total_count_data= cursor.fetchone()
                    if cursor.rowcount<=0:
                        sql="insert into play_url_list_count(movie,sources,spider_source_index,totalCount) values({0},{1},{2},0)".format(movie_id,source_id,sources_index)
                        cursor.execute(sql)
                        session.commit()
                    else:
                        m_url_offset= int(m_total_count_data[4])
                    if m_url:
                        play_urls = m_url.split('|')
                        if m_url_offset-1<0:
                            m_url_offset = 1
                        if len(play_urls)>=m_url_offset:
                            for play_url in play_urls[(m_url_offset-1):]:
                                m_p = play_url.split("$")
                                play_url = m_p[1]
                                if play_url:
                                    play_url = play_url.lstrip().rstrip() #去除左右空格.
                                    sql="select *from movie_play_list where spider_sources_index={0} and movie_id={1} and sources={2} and p_index='{3}'".format(sources_index,movie_id,source_id,m_p[0])
                                    #sql = "select *from movie_play_list where play_url=\"{0}\" and spider_sources_index={1}".format(play_url, sources_index)
                                    cursor.execute(sql)
                                    data = cursor.fetchall()
                                    if cursor.rowcount <= 0:
                                        sql = "insert into movie_play_list(movie_id,play_url,sources,p_index,spider_sources,spider_sources_index) values({0},\"{1}\",\"{2}\", \"{3}\",\"{4}\",{5})".format(
                                            movie_id, play_url, source_id, m_p[0], sources, sources_index)
                                        cursor.execute(sql)
                                        session.commit()
                                    else:
                                        sql="update movie_play_list set play_url='{4}' where spider_sources_index={0} and movie_id={1} and sources={2} and p_index='{3}'".format(sources_index,movie_id,source_id,m_p[0],play_url)
                                        cursor.execute(sql)
                                        session.commit()
                            sql="update play_url_list_count set totalCount={0} where sources={1} and spider_source_index={2} and movie={3}".format(len(play_urls),source_id,sources_index,movie_id)
                            cursor.execute(sql)
                            session.commit()
                    source_id += 1
        except Exception as e:
            logging.error(e)
            logging.error(sql)
        finally:
            cursor.close()
            session.close()
            db_event.set()
    else:
        db_event.clear()
        db_event.wait()
        saveMsg(**kwargs)


def updateTime():
    '''初次采集后调用更新资源更新时间顺序'''
    session = db_helper.getSession()
    if session:
        try:
            cursor = session.cursor()
            sql = "SELECT * from movie_list order by last_update_time desc"
            cursor.execute(sql)
            for item in cursor.fetchall():
                sql = "update movie_list set last_update_time=\"{0}\" where id={1}".format(
                    datetime.datetime.now(), item[0])
                print(sql)
                cursor.execute(sql)
                session.commit()
        finally:
            cursor.close()
            session.close()

'''
config = {
    "host": DATABASES['default']['HOST'],
    "port": DATABASES['default']['PORT'],
    "user": DATABASES['default']['USER'],
    "password": DATABASES['default']['PASSWORD'],
    "database": DATABASES['default']['NAME']
}

db_helper = db_pool(config, 32)
'''